package org.open.sharedRadar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Options extends Activity{
	DBAdapter myDBAdapter = new DBAdapter(this);
	public DBAdapter getMyDBAdapter() {
		return myDBAdapter;
	}
	public void setMyDBAdapter(DBAdapter myDBAdapter) {
		this.myDBAdapter = myDBAdapter;
	}
	int selectedRadius;
	int selectedInterval;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.options);

	    Spinner radiusSpinner = (Spinner) findViewById(R.id.radiusSpinner);
	    ArrayAdapter<CharSequence> radiusAdapter = ArrayAdapter.createFromResource(
	            this, R.array.radius_array, android.R.layout.simple_spinner_item);
	    radiusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    radiusSpinner.setAdapter(radiusAdapter);
	    radiusSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
		{
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
		    {		    						
		    	selectedRadius = position;	
		    }

			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	    
	    Spinner intervalSpinner = (Spinner) findViewById(R.id.intervalSpinner);
	    ArrayAdapter<CharSequence> intervalAdapter = ArrayAdapter.createFromResource(
	            this, R.array.interval_array, android.R.layout.simple_spinner_item);
	    intervalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    intervalSpinner.setAdapter(intervalAdapter);
	    intervalSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
		{
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
		    {							
		    	selectedInterval = position;						    	
		    }

			public void onNothingSelected(AdapterView<?> arg0) {}
		});

	    Button buttonOk = (Button) findViewById(R.id.btnOk);
	    buttonOk.setOnClickListener(new OnClickListener(){
	    	public void onClick(View v) {			
	 		   try
	 		   {
	 			  switch(selectedInterval)
	 				{
	 					case 0:
	 						CurentUserProperties.getInstance().myInterval = 1;
	 						break;
	 						
	 					case 1:
	 						CurentUserProperties.getInstance().myInterval = 10;
	 						break;
	 					case 2:
	 						CurentUserProperties.getInstance().myInterval = 60;
	 						break;
	 				}
	 			 switch(selectedRadius)
	 				{
	 					case 0:
	 						CurentUserProperties.getInstance().myRadius = 5000;
	 						break;
	 						
	 					case 1:
	 						CurentUserProperties.getInstance().myRadius = 10000;
	 						break;
	 					case 2:
	 						CurentUserProperties.getInstance().myRadius = 20000;
	 						break;
	 					case 3:
	 						CurentUserProperties.getInstance().myRadius = 100000;
	 						break;
	 				}
	 			   	 			   
	 			   
	 			   myDBAdapter.open();
	 			   myDBAdapter.parseUserDataToFromDB(true);
	 			   myDBAdapter.close();	 			   
	 			   	finish();
	 		   }
	 		   catch (Exception aE)
	 		   {
	 		       aE.printStackTrace ();;
	 		   }
	 			
	 		}
	    });     	    
	}	
	
}
